package chapter15;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description: 数据库对应到实体类属性列表
 *
 * @author lzy
 * @create 2018-03-26 下午4:06
 */
public class PoMappingDo {
    // 映射格式为- 列表名：属性名
    // key:实体类属性名称转小写 value：实体类属性名称
    private static Map<Class<?>, Map<String, String>> attributeMap = new HashMap<>();

    public static Map<String, String> getAttributeMap(Class<?> clazz) {
        Map<String, String> map = new HashMap<>(16);
        if (attributeMap.containsKey(clazz)) {
            return attributeMap.get(clazz);
        }
        return getAttributeMapByClass(clazz);
    }

    private static Map<String, String> getAttributeMapByClass(Class<?> clazz) {
        Map<String, String> map = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            map.put(field.getName().toLowerCase(), field.getName());
        }
        attributeMap.put(clazz, map);
        return map;
    }
}
